// pages/studyRecord/studyRecordList/studyRecordList.js
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageName: '学习记录',
    imageurl: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/',
    page: 1,
    pageSize: 20,
    hasMoreData: false,
    studyRecordList: [],
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    that.getStudyRecordList();
  },

 /**
   * 获取学习记录列表
   */
  getStudyRecordList:function(){
   var that = this;
    wx.showLoading({
      title: '',
      mask: true
    })

    let bodyParam = {
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      page: that.data.page,
      size: that.data.pageSize
    }
    httpUtils.getStudyRecordList(bodyParam).then((res) => {
      var studyRecordList = that.data.studyRecordList;
      var list = res.data.list;
      if (that.data.page == 1) {
        studyRecordList = []
      }

      if (list.length < that.data.pageSize) {
        that.setData({
          studyRecordList: studyRecordList.concat(list),
          hasMoreData: false
        })
      } else {
        that.setData({
          studyRecordList: studyRecordList.concat(list),
          hasMoreData: true,
          page: that.data.page + 1
        })
      }

    });
  },

  //点击记录，跳转学习记录详情界面
  toStudyRecordDetail:function(e){
    var that = this;
    let index = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/studyRecord/studyRecord?date=' + that.data.studyRecordList[index].date,
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 1
    })
    that.getStudyRecordList();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (this.data.hasMoreData) {
      that.getStudyRecordList();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})